package mybox.engine;

import java.io.IOException;

public class Arduino implements Figur {
	
	public File4Qcad file4qcad=null;
	QVector lowerLeft=null;
	QVector upperRight=null;
	
	Rectangle basicRectangle;
	
	public static final double laenge=68.925;	
	public static final double breite=53.65;
	public static final double standardHoehe=22;
	
	public static final double bfSchraubenRadius=1.6;
	
	public static final double yabstand2BoxRand=8;
	public static final double xabstand2BoxRand=5;
	
	double upperLeftBFCenterX;
	double upperLeftBFCentermY;
	QVector upperLeftBFSchraubenCenter;
	
	
	double lowerRightBFCenterY;
	double lowerRightBFCentermX;
	QVector lowerRightBFSchraubenCenter;
	
	Rectangle usbAnschluss;
	public static final double usbYOffset=8.0;
	public static final double usbUeLaenge=6.5;
	public static final double usbBreite=13.7;
	public static final double usbHoehe=15.8;	
	public static final double usbLuft2Boden=3.8;
	
	Rectangle powerAnschluss;
	public static final double powerYmOffset=4.2;
	public static final double powerBreite=8.7;
	public static final double powerUeLaenge=2;
	public static final double powerHoehe=14.5;
	public static final double powerLuft2Boden=4.3;
		
	Schachtel arduinoBox;
	double ardLuft2wand;
	double materialDicke;
	double zinkungsFaktor;
	double laserBreite;
	
	double ardBoxLaenge;
	double ardBoxBreite;
	double ardBoxHoehe;	
	
	Circle befestigungUpperLeft, befestigungLowerRight;
	
	public static QVector calcLowerLeftFromUpperRight(QVector upperRight) {
		QVector retVec = new QVector(upperRight.x-laenge, upperRight.y-breite);
		return retVec;
	}
	
	
	public Arduino(File4Qcad file4qcad, QVector lowerLeft, double materialDicke, double zinkungsFaktor,double laserBreite) {
		this.file4qcad=file4qcad;
		this.lowerLeft=lowerLeft;
		
			
		
		this.upperRight=new QVector(lowerLeft.x+laenge,lowerLeft.y+breite);			
		basicRectangle = new Rectangle(file4qcad,lowerLeft,upperRight);
		
		upperLeftBFCenterX=1;
		upperLeftBFCentermY=6;
		upperLeftBFSchraubenCenter = new QVector(
				basicRectangle.upperLeft.x+upperLeftBFCenterX+bfSchraubenRadius,
				basicRectangle.upperLeft.y-(upperLeftBFCentermY+bfSchraubenRadius)
				);
		
		lowerRightBFCenterY=1;
		lowerRightBFCentermX=13.65;
		lowerRightBFSchraubenCenter = new QVector(
				basicRectangle.lowerRight.x-lowerRightBFCentermX-bfSchraubenRadius,
				basicRectangle.lowerRight.y+lowerRightBFCenterY+bfSchraubenRadius
				);
		
		
		
		usbAnschluss = new Rectangle(file4qcad,
		               		new QVector(basicRectangle.lowerRight.x,basicRectangle.lowerRight.y+usbYOffset),
		               		new QVector(basicRectangle.lowerRight.x+usbUeLaenge,basicRectangle.lowerRight.y+usbYOffset+usbBreite)
		);
		
		
		
		powerAnschluss = new Rectangle(
				file4qcad,
				new QVector(basicRectangle.upperRight.x,basicRectangle.upperRight.y-powerYmOffset-powerBreite),
           		new QVector(basicRectangle.upperRight.x+powerUeLaenge,basicRectangle.upperRight.y-powerYmOffset)
				);
		
		this.materialDicke=materialDicke;
		this.zinkungsFaktor=zinkungsFaktor;
		this.laserBreite=laserBreite;
		ardLuft2wand=10;
		//???????
		ardBoxLaenge=68+2*materialDicke+2*ardLuft2wand;
		ardBoxBreite=54+2*materialDicke+2*ardLuft2wand;
		ardBoxHoehe=22+2*materialDicke;
		
		befestigungUpperLeft = new Circle(file4qcad, upperLeftBFSchraubenCenter,bfSchraubenRadius);
		befestigungLowerRight = new Circle(file4qcad, lowerRightBFSchraubenCenter,bfSchraubenRadius);
		
	    
		
	}
	
	public void draw() {
		basicRectangle.draw();		
		usbAnschluss.draw();
		powerAnschluss.draw();
		befestigungUpperLeft.draw();
		befestigungLowerRight.draw();		
	}
	
	public void drawBox() throws SecurityException, IOException {
		
		basicRectangle.draw();		
		usbAnschluss.draw();
		powerAnschluss.draw();		
		befestigungUpperLeft.draw();
		befestigungLowerRight.draw();	
		
		
		
		arduinoBox = new Schachtel(file4qcad, new QVector(
												basicRectangle.lowerLeft.x-materialDicke-ardLuft2wand,
												basicRectangle.lowerLeft.y-materialDicke-ardLuft2wand), 
												ardBoxLaenge,ardBoxBreite,ardBoxHoehe,materialDicke,zinkungsFaktor,laserBreite);
		
		
		
	}
	
	public void drawArduinoPlusPololu(boolean befestigungPololu) throws SecurityException, IOException {
		
				
		double fillerSpace=5;
		QVector pololuLowerLeft = new QVector(basicRectangle.lowerRight.x-PololuMSSC.laenge-4,
											  basicRectangle.lowerRight.y-PololuMSSC.breite-fillerSpace);
		PololuMSSC pololuMSSC = new PololuMSSC(file4qcad, pololuLowerLeft, materialDicke, laserBreite);
		if (befestigungPololu) {
			pololuMSSC.draw();		
			befestigungUpperLeft.draw();
			befestigungLowerRight.draw();
		} else {
			pololuMSSC.drawUsServo();
		}
		ardBoxLaenge=laenge+2*materialDicke+2*ardLuft2wand;
		ardBoxBreite=breite+PololuMSSC.breite+fillerSpace+2*materialDicke+2*ardLuft2wand;
		ardBoxHoehe=22+2*materialDicke;
		
		arduinoBox = new Schachtel(file4qcad, new QVector(
												basicRectangle.lowerLeft.x-materialDicke-ardLuft2wand,
												basicRectangle.lowerLeft.y-materialDicke-ardLuft2wand-PololuMSSC.breite-fillerSpace), 
												ardBoxLaenge,ardBoxBreite,ardBoxHoehe,materialDicke,zinkungsFaktor,laserBreite);
							
		
	}
	
	public void drawUSB() {
		usbAnschluss.draw();
	}
	
	public void drawPower() {
		powerAnschluss.draw();
	}


}
